<?php
/*
 * @Author: CYH
 * @Date: 2020-10-31 10:25:06
 * @LastEditTime: 2021-04-22 10:36:28
 * @LastEditors: Please set LastEditors
 * @Description: 宿舍管理model类
 * @FilePath: \dorm_manage_DEV\application\models\Dorm\Dorm_manage.php
 */
    // 宿舍管理model类
    class Dorm_manage extends CI_Model
    {
        // 构造方法
        public function __construct()
        {

            parent::__construct();
            $this->load->database();
        }

        /**
         * @description: 宿舍楼信息管理新增方法
         * @param {*}
         * @return {area_number, floor_name, floorNumber, floor_cname, adminName, liveNumber, remarks, floor_location}
         */        
        public function dorm_insert($area_number, $floor_name, $floorNumber, $floor_cname, $adminName, $remarks, $floor_location)
        {
            //若区域编号和宿舍楼名称为空则报错
            if ($area_number == '' || $floor_name == '') 
            {
                $resturn_fa = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
                $result = json_encode($resturn_fa);
            } 
            else 
            {
                //取宿舍楼编号的最大值
                $this->db->select_max('floor_number');
                $query = $this->db->get('dorm_sslmanage');
                //宿舍楼编号自动递增
                $number = $query->row()->floor_number + 1;
                //去当前的日期
                $date_now = date("Y-m-d");
                //通过区域编号取区域名
                $query_name = $this->db->get_where('dorm_area', array('area_number' => $area_number));
                //去插入表的值
                $data = array(
                    'area_name' => $query_name->row()->area_name,
                    'area_number' => $area_number,          //区域编号
                    'floor_number' => $number,              //宿舍楼编号
                    'floor_name' => $floor_name,            //宿舍楼名称
                    'floor_location' => $floor_location,    //宿舍楼地址
                    'floor_cname' => $floor_cname,          //创建人
                    'floor_cdate' => $date_now,             //创建日期
                    'floorNumber' => $floorNumber,          //宿舍楼层数
                    'adminName' => $adminName,              //宿管员姓名
                    // 'liveNumber' => $liveNumber,         //入住人数
                    'remarks' => $remarks                   //备注
                );
                //将新增数据插入表中
                $this->db->insert('dorm_sslmanage', $data);

                $return = $this->db->affected_rows();

                if ($return) 
                {
                    //新增成功
                    $resturn_su = array('res' => '40000', 'msg' => '新增成功' ,'data' => '');
                    $result = json_encode($resturn_su);
                } 
                else 
                {
                    //新增失败
                    $resturn_fa = array('res' => '40001', 'msg' => '新增失败' ,'data' => '');
                    $result = json_encode($resturn_fa);
                }
            } 
            return $result;
        }

        /**
         * @description: 宿舍信息管理删除方法
         * @param {area_number, floor_number}
         * @return {*}
         */        
        public function dorm_delete($area_number, $floor_number)
        {
            //取要删除的宿舍得居住人数
            $query = $this->db->get_where('dorm_sslmanage', array('area_number' => $area_number, 'floor_number' => $floor_number));
            //如果宿舍得居住人数不为零不允许删除
            if ($query->row()->liveNumber != 0) 
            {
                $return_fa = array('res' => '40001', 'msg' => '住宿人数不为零不允许删除' ,'data' => '');
                $result = json_encode($return_fa);
            } 
            else 
            {
                //按区域编号和宿舍楼编号删除表中的数据
                // $where = array('area_number' => $area_number, 'floor_number' => $floor_number);
                // $this->db->where($where);
                $this->db->delete('dorm_sslmanage' ,array('area_number' => $area_number, 'floor_number' => $floor_number));
                $return = $this->db->affected_rows();
                if ($return) 
                {
                    //删除成功
                    $return_su = array('res' => '40000', 'msg' => '删除成功' ,'data' => '');
                    $result = json_encode($return_su);
                } 
                else 
                {
                    //删除失败
                    $return_fa = array('res' => '40001', 'msg' => '删除成功' ,'data' => '');
                    $result = json_encode($return_fa);
                }
            }
            return $result;
        }
        
        /**
         * @description: 取宿舍楼层数
         * @param {area_number, floor_number}
         * @return {*}
         */
        public function dormFloor($area_number, $floor_number)
        {
            $query = $this->db->get_where('dorm_sslmanage', array('area_number' => $area_number, 'floor_number' => $floor_number));
            if($query->num_rows()>0)
            {
                //查询成功
                $return_su = array('res' => '40000', 'msg' => '查询成功' ,'data' => $query->row()->floorNumber);
                $result = json_encode($return_su);
            }
            else
            {
                //查询失败
                $return_fa = array('res' => '40001', 'msg' => '查询无数据' ,'data' => '');
                $result = json_encode($return_fa);
            }
            return $result;

        }

        /**
         * @description: 检查宿舍楼是否住人
         * @param {area_number, floor_number}
         * @return {*}
         */
        public function liveCheck($area_number, $floor_number)
        {
            $query = $this->db->get_where('dorm_bed', array('area_number' => $area_number, 'floor_number' => $floor_number));
            if($query->num_rows()>0)
            {
                //查询成功
                $return_su = array('res' => '40000', 'msg' => '查询成功' ,'data' => '');
                $result = json_encode($return_su);
            }
            else
            {
                //查询失败
                $return_fa = array('res' => '40001', 'msg' => '查询无数据' ,'data' => '');
                $result = json_encode($return_fa);
            }
            return $result;
        }

        /**
         * @description: 宿舍楼信息管理修改方法
         * @param {area_name, area_number, floor_name, floor_number, floor_location, floorNumber, floor_cname, floor_cdate, remarks, adminName, liveNumber}
         * @return {*}
         */        
        public function dorm_update($area_name, $area_number, $floor_name, $floor_number, $floor_location, $floorNumber, $floor_cname, $floor_cdate, $remarks, $adminName, $liveNumber)
        {
            //如果区域名称为空则报错
            if ($area_name == '') 
            {
              $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
              $result = json_encode($return);
            } 
            else 
            {
                //取原宿舍楼层数
                $dormFloor = $this->dormFloor($area_number, $floor_number);
                $dormFloor = json_decode($dormFloor);
                //若原宿舍楼层数与传进来的宿舍楼层数不同，则判断宿舍楼是否有人居住，若居住则不允许修改
                if($dormFloor->data != $floorNumber)
                {
                    $liveCheck = $this->liveCheck($area_number, $floor_number);
                    $liveCheck = json_decode($liveCheck);
                    if($liveCheck->res == '40000')
                    {
                        $resturn_fa = array('res' => '40003', 'msg' => '宿舍楼有人居住，不允许修改楼层', 'data' => '');
                        $result = json_encode($resturn_fa);
                        return $result;
                    }
                }

                //去更新的数据
                $data = array(
                    'area_name' => $area_name,          //区域名称
                    'area_number' => $area_number,      //区域编号
                    'floor_name' => $floor_name,        //宿舍楼名称
                    'floor_number' => $floor_number,    //宿舍楼编号
                    'floor_location' => $floor_location,//宿舍楼地址
                    'floorNumber' => $floorNumber,      //宿舍楼层数
                    'floor_cname' => $floor_cname,      //创建人
                    'floor_cdate' => $floor_cdate,      //创建日期
                    'remarks' => $remarks,              //备注
                    'adminName' => $adminName,          //宿管员姓名
                    'liveNumber' => $liveNumber         //入住人数
                );
                //用数据更新表
                $where = array('area_number' => $area_number, 'floor_number' => $floor_number);
                $this->db->where($where);
                $query1 = $this->db->update('dorm_sslmanage', $data);
                $return = $this->db->affected_rows();
                $query = $this->db->get_where('dorm_sslmanage', array('area_number' => $area_number, 'floor_number' => $floor_number));

                if ($return) 
                {
                    //更改成功
                    $resturn_su = array('res' => '40000', 'msg' => '修改成功', 'data' => $query->row());
                    $result = json_encode($resturn_su);
                } 
                else 
                {
                    //更改失败
                    $resturn_fa = array('res' => '40001', 'msg' => '修改失败' ,'data' => '');
                    $result = json_encode($resturn_fa);
                }
            }
            return $result;
        }

        /**
         * @description: 宿舍楼信息管理搜索方法
         * @param {area_number, floor_number, adminName, limit}
         * @return {*}
         */        
        public function dorm_select($area_number, $floor_number, $adminName, $limit)
        {
            //拼接sql语句
            $sj1 = $area_number == '' ? ' 1 = 1 ' : " area_number LIKE '%" . $area_number . "%'";
            $sj2 = $floor_number == '' ? ' AND 1 = 1 ' : " AND floor_number LIKE '%" . $floor_number . "%'";
            $sj3 = $adminName == '' ? ' AND 1 = 1 ' : " AND adminName LIKE '%" . $adminName . "%'";
            if($limit != '')
            {
                $sql = 'SELECT * FROM dorm_sslmanage WHERE' . $sj1 . $sj2 . $sj3 . " Limit " . ($limit - 1) * 10 . ",10";
            } 
            else 
            {
                $sql = 'SELECT * FROM dorm_sslmanage WHERE' . $sj1 . $sj2 . $sj3;
            }
            $query = $this->db->query($sql, $sj1, $sj2, $sj3);
            // $sql1 = 'SELECT * FROM dorm_sslmanage';
            // $query1 = $this->db->query($sql1);
            if ($query->num_rows() > 0) 
            {
                //查询成功
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result() ,'page' => $query->num_rows());
                $result = json_encode($return_su);
            } 
            else 
            {
                //查询失败
                $return_fa = array('res' => '40001', 'msg' => '查询无数据', 'data' => '' ,'page' => '');
                $result = json_encode($return_fa);
            }
            return $result;
          }

        /**
         * @description:宿舍人员信息excel导出查询 
         * @param {*}
         * @return {*}
         */      
        public function excel_dorm()
        {
            $query = $this->db->query("SELECT B.area_name ,C.floor_name ,A.dorm_floor ,A.room_number ,A.name ,A.sex ,A.jobNumber ,A.idNumber ,A.degsonid ,A.department ,A.job ,A.location ,A.address
                                        ,A.level ,A.classes ,A.phone ,A.emergencyContact ,A.relationShip ,A.emergencyPhone ,A.liveCheck ,A.liveDate ,A.quitCheck ,A.retreatType
                                        ,A.retreatDate ,A.repetitionCheck ,A.blacklistCheck ,A.bedNumber ,A.wardrobe ,A.freeCheck ,A.freeDays ,A.temporary ,A.remarks FROM dorm_livepeople AS A
                                        LEFT JOIN dorm_area AS B ON A.area_number = B.area_number
                                        LEFT JOIN dorm_sslmanage AS C ON A.area_number = C.area_number AND A.floor_number = C.floor_number");
            $result = $query->result();
            return json_encode(array('ret' => '40001', 'msg' => $result));
        }

        /**
         * @description: 宿舍楼信息excel导出查询
         * @param {*}
         * @return {*}
         */      
        public function excel_dorm_information()
        {
            $query = $this->db->query("SELECT area_name ,area_number ,floor_number ,floor_name ,floor_location ,floor_cname ,floor_cdate ,floorNumber ,adminName ,liveNumber ,remarks FROM dorm_sslmanage");
            $result = $query->result();
            return json_encode(array('ret' => '40001', 'msg' => $result));
        }
}
